/* 
 * File:   main.cpp
 * Author: max
 *
 * Created on October 19, 2012, 9:29 PM
 */

#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include "client_Cliente.h"

using namespace std;
using namespace TP::Comunicacion;

#define SUCCESS_EXIT 0
#define CONNECTION_FAILED 1
#define INVALID_SERVICE_REFERENCE 2
#define INVALID_ARGS 3


int main(int argc, char** argv) {
    
    int val = SUCCESS_EXIT;
    
    try
    {
        // Verifico la cantidad de parametros de la aplicacion.
        if (argc != 2)
            throw INVALID_ARGS;

        // Interpreto la referencia al servicio de los parametros.
        string item = "";
        vector<string> comando;
        std::stringstream ss (argv[1]);

        while (std::getline(ss, item, ':' ))
            comando.push_back(item);

        if (comando.size() != 2)
            throw INVALID_SERVICE_REFERENCE;

        Cliente cliente (comando[0], comando[1], cout);
        cliente.Iniciar();

        try
        {
            while (std::getline(std::cin, item, '\n'))
            {
                if (item != "S")
                    cliente.enviar(item);
                else
                    break;
            }
        }
        catch (Excepcion& pEx)
        {
            cout << pEx.what() << endl;
        }
        cliente.Finalizar();
    }
    catch (Excepcion& pEx)
    {
        cout << pEx.what() << endl;
        val = CONNECTION_FAILED;
    }
    catch (int pEx)
    {
        val = pEx;
    }

    return val;
}

